from annoying.decorators import render_to
from django.db.models import Count

from core.models import Event, User
from core.lib import is_user_authenticated


@render_to('index.html')
def index(request):
    recomendations_by_cost = None
    recomendations_by_participants = None
    if is_user_authenticated(request):
        user = User.objects.get(id=request.session['user_id'])
        events = Event.objects.filter(status=Event.Status.Scheduled, \
                                      city__in=user.cities.all()).exclude(participants=user)
        recomendations_by_cost = events.order_by('-current_sum_for_user')
        recomendations_by_participants = events.annotate(participants_count=Count('participants')).order_by('-participants_count')
    return {'recomendations_by_cost': recomendations_by_cost, \
            'recomendations_by_participants': recomendations_by_participants}
